You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[customId].tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { Grid, Typography } from '@mui/material';
  2. import { Box, Container } from '@mui/system';
  3. import { dehydrate, QueryClient } from '@tanstack/react-query';
  4. import { NextPage } from 'next';
  5. import { useTranslation } from 'next-i18next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import Image from 'next/image';
  8. import { useRouter } from 'next/router';
  9. import React from 'react';
  10. import GridItem from '../../components/grid-item/GridItem';
  11. import Loader from '../../components/loader/Loader';
  12. import ProductCard from '../../components/product-card/ProductCard';
  13. import TabContent from '../../components/tab-content/TabContent';
  14. import { useFetchSingleProduct } from '../../hooks/useFetchProductData';
  15. import { getProductData } from '../../requests/products/producDataRequest';
  16. import { useStore, useStoreUpdate } from '../../store/cart-context';
  17. import { SingleProductResponseGet } from '../../utils/interface/productInterface';
  18. interface Props {
  19. data: SingleProductResponseGet;
  20. }
  21. const SingleProduct: NextPage<Props> = ({ data }) => {
  22. const { t } = useTranslation('products');
  23. const { addCartValue } = useStoreUpdate();
  24. const { cartStorage } = useStore();
  25. const router = useRouter();
  26. const customId = router.query.customId ? router.query.customId as string : undefined;
  27. // const { data, isLoading } = useFetchSingleProduct(customId);
  28. const addProductToCart = (quantity: number) => addCartValue(data?.product, quantity);
  29. const inCart =
  30. cartStorage?.length > 0
  31. ? cartStorage?.some((item) => item.product.customID === data?.product.customID)
  32. ? true
  33. : false
  34. : false;
  35. return (
  36. <Box
  37. sx={{
  38. display: 'flex',
  39. flexDirection: 'column',
  40. }}
  41. >
  42. <Container>
  43. <Typography
  44. fontFamily={'body1.fontFamily'}
  45. fontSize="32px"
  46. sx={{ mt: 25, height: '100%', color: 'primary.main' }}
  47. >
  48. {data?.product.name}
  49. </Typography>
  50. <Grid container spacing={2}>
  51. <Grid sx={{ display: 'flex' }} item md={6} sm={12}>
  52. <Image
  53. src={data?.product.image}
  54. alt="product"
  55. width={900}
  56. height={700}
  57. />
  58. </Grid>
  59. <TabContent
  60. description={data?.product.description}
  61. inCart={inCart}
  62. price={data?.product.price}
  63. category={data?.product.category}
  64. addProductToCart={addProductToCart}
  65. />
  66. </Grid>
  67. <Typography
  68. sx={{
  69. mt: { xs: '60px', md: '100px', lg: '150px' },
  70. mb: 5,
  71. color: 'primary.main',
  72. fontSize: '32px',
  73. }}
  74. >
  75. {t('products:similar')}
  76. </Typography>
  77. <Grid container spacing={2}>
  78. {data?.similarProducts.map((product) => (
  79. <GridItem key={product._id}>
  80. <ProductCard product={product} />
  81. </GridItem>
  82. ))}
  83. </Grid>
  84. </Container>
  85. </Box>
  86. );
  87. };
  88. export const getServerSideProps = async (context: any) => {
  89. const { params } = context;
  90. const { customId } = params;
  91. console.log(customId);
  92. const queryClient = new QueryClient();
  93. const data = await queryClient.fetchQuery(
  94. ['product', customId],
  95. async () => await getProductData(customId)
  96. );
  97. await queryClient.fetchQuery(
  98. ['product', customId],
  99. async () => await getProductData(customId)
  100. );
  101. return {
  102. props: {
  103. data: data,
  104. dehydratatedState: dehydrate(queryClient),
  105. ...(await serverSideTranslations(context.locale, ['products'])),
  106. },
  107. };
  108. };
  109. export default SingleProduct;